package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.DotorAdapter;
import com.zmx.doctor.adapter.GroupingAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.GoodsOrder;
import com.zmx.doctor.pojo.Grouping;
import com.zmx.doctor.pojo.SearchDoctor;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/12/3  13:58
 * 功 能：新增患者
 */
public class AddPatientsActivity extends BaseActivity {

    @BindView(R.id.reView)
    RecyclerView reView;
    @BindView(R.id.no_data)
    TextView noData;
    private DotorAdapter adapter;
    private List<SearchDoctor> lists;


    private List<Grouping> dialog_lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_patients;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        lists = new ArrayList<>();
        dialog_lists = new ArrayList<>();
        adapter = new DotorAdapter(mActivity, handler, 3);
        reView.setLayoutManager(new LinearLayoutManager(this));
        reView.setAdapter(adapter);

        group();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    lists.clear();

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Gson gson = new Gson();
                        JSONArray array = object.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {

                            SearchDoctor doctor = gson.fromJson(array.getJSONObject(i).toString(), SearchDoctor.class);
                            lists.add(doctor);

                        }

                        if (lists.size()>0){
                            noData.setVisibility(View.GONE);
                        }else{
                            noData.setVisibility(View.VISIBLE);
                        }

                        adapter.setListAndNotifyDataSetChanged(lists);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 2:

//                    addUser(msg.obj.toString());

                    if (dialog_lists.size()>0){

                        showPhoto(msg.obj.toString());

                    }else{
                        Toast("没有分组，请先添加分组");
                    }

                    break;

                case 3:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 4:

                    Log.e("方法是", "发生发" + msg.obj.toString());

                    try {
                        dialog_lists.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            Grouping g = gson.fromJson(array.getJSONObject(i).toString(), Grouping.class);
                            dialog_lists.add(g);

                        }

                        getAllowPatient();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;


            }

        }
    };

    private GroupingAdapter dialog_adapter;
    private RecyclerView dialog_reView;
    private Dialog modify_dialogs;//弹出框
    public void showPhoto(String uid) {

        View view;//选择性别的view

        modify_dialogs = new Dialog(mActivity, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_grouping, null);
        //将布局设置给Dialog
        modify_dialogs.setContentView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = modify_dialogs.getWindow();

        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //        lp.y = 20;//设置Dialog距离底部的距离

        //// 以下这两句是为了保证按钮可以水平满屏
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        modify_dialogs.onWindowAttributesChanged(lp);
        //       将属性设置给窗体
//        modify_dialogs.setCanceledOnTouchOutside(false);
        modify_dialogs.show();//显示对话框

        dialog_reView = view.findViewById(R.id.reView);
        dialog_reView.setLayoutManager(new LinearLayoutManager(this));
        dialog_adapter = new GroupingAdapter(this);
        dialog_adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                senDing(uid,dialog_lists.get(listPosition).getId()+"");
                modify_dialogs.dismiss();

            }
        });
        dialog_reView.setAdapter(dialog_adapter);
        dialog_adapter.setListAndNotifyDataSetChanged(dialog_lists);

    }


    public void getAllowPatient() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getAllowPatient");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.patient_follow/entry", params, handler, 1, 404);

    }


    public void senDing(String user_id,String group_id) {

        List<String> ls = new ArrayList<>();
        ls.add(group_id);
        ls.add(user_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "senDing");
        params.put("data[patient_id]", user_id);
        params.put("data[group_id]", group_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.patient_follow/entry", params, handler, 3, 404);

    }


    public void group() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "group");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 4, 404);

    }


}